package com.blackcat.demo.sm;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;

/**
 * 描述 :国密sm4
 * @author : zhangdahui  2024/5/15 下午2:00
 * @version 1.0
 */
public class SM4Dome {
    public static void main(String[] args) {
        // 注意：加解密的key的长度要是16个字符，否则会失败
        String key = "04a477628f555bdb";

        String password = "123456.123";
        String encryptStr = encrypt(key, password);
        System.out.println("加密 " + encryptStr);
        String decryptStr = decrypt(key, encryptStr);
        System.out.println("解密 " + decryptStr);
    }

    /**
     * 加密
     *
     * @param key 秘钥
     * @param data 明文
     * @return 密文
     */
    public static String encrypt(String key, String data) {
        SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", key.getBytes());
        return sm4.encryptHex(data).toUpperCase();
    }

    /**
     * 解密
     *
     * @param key 秘钥
     * @param data 密文
     * @return 明文
     */
    public static String decrypt(String key, String data) {
        SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", key.getBytes());
        return sm4.decryptStr(data);
    }



}
